Skip to content

extract: Warn when skipping an f-string with substitutions - #1343

Open
ColinHouse wants to merge 1 commit into
python-babel:masterfrom
ColinHouse:warn-skipped-fstring
Open

extract: Warn when skipping an f-string with substitutions#1343
ColinHouse wants to merge 1 commit into
python-babel:masterfrom
ColinHouse:warn-skipped-fstring

Conversation

@ColinHouse

Copy link
Copy Markdown

_(f"Hello {name}") cannot be translated, so extraction skips it. Since #915
replaced eval with ast, it is skipped silently: pybabel extract exits 0,
writes no message, and prints nothing. The string never reaches translators and
ships untranslated, with no signal to the author.

This warns instead, in the same format as the existing "Empty msgid" warning:

app.py:2: warning: Skipping f-string with substitutions: it cannot be
translated as written.  Extract a format string instead,
e.g. _("Hello {name}").format(name=name).

There are two code paths, because f-string tokenisation changed in 3.12:

  • before 3.12 the whole f-string is one STRING token and _parse_python_string
    returns None;
  • from 3.12 (PEP 701) a substitution arrives as a non-FSTRING_* token and the
    f-string is dropped where current_fstring_start is reset.

Both are covered, and the suite was run on 3.11 and 3.12.

It warns rather than raising, as the issue body asks ("warn the user about the
incorrect usage instead of simply crashing"). Raising would break builds that
extract successfully today. Happy to make it an error if you prefer.

A constant f-string such as _(f"spam" f"eggs") is still extracted and does not
warn; an f-string outside a translation call is untouched.

The stale # TODO: we could raise an error or warning when not all nodes are constants is replaced, since this implements it.

fixes #715

An f-string that interpolates values cannot be translated, so extraction
skips it. Since python-babel#915 replaced eval with ast it is skipped silently: the
message never reaches translators, and nothing tells the author.

Warn instead, in the same format as the existing "Empty msgid" warning.
Both tokenisation paths are covered: before Python 3.12 the whole f-string
is a single STRING token, and from 3.12 (PEP 701) a substitution arrives
as a non-FSTRING_* token and the f-string is dropped where
current_fstring_start is reset.

Fixes python-babel#715
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pybabel should raise an error when it encounters f-strings

1 participant